/**
 * Copyright (C) 2018 Luoran, Inc. All Rights Reserved.
 */
package com.luoran.kaka.action;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.luoran.kaka.entity.BasicInfo;
import com.luoran.kaka.entity.PicInfo;
import com.luoran.kaka.entity.Test;
import com.luoran.kaka.service.IBasicInfoService;
import com.luoran.kaka.service.IPicInfoService;
import com.luoran.kaka.service.ITestService;

/**
 * @author ghl  2018 2018年11月19日
 *
 */
@Controller
@RequestMapping("/test")
public class TestAction {
	@Autowired 
	private ITestService testService;
	@Autowired 
	private IBasicInfoService basicInfoService;
	@Autowired
	private IPicInfoService picInfoService;
	
	@RequestMapping("/test")
	public String test(Model m) {
		List<Test> list = testService.query();
		m.addAttribute("list", list);
		return "test";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public List<Test> query(){
		List<Test> list = testService.query();
		return list;
	}
	
	@RequestMapping("add")
	@ResponseBody
	public Test add() {
		Test test = new Test();
		test.setName("张三");
		test.setAge(new Random().nextInt(20));
		String id = testService.add(test);
		System.err.println("新增记录ID为:"+id);
		return test;
	}
	
	@RequestMapping("conditionQuery")
	@ResponseBody
	public List<Test> conditionQuery(){
		String name = "张三";
		List<Test> list = testService.conditionQuery(name);
		return list;
	}
	
	@RequestMapping("/fail")
	public String failPage() {
		return "common/fail";
	}
	
	@RequestMapping("/success")
	public String successPage(String msg , Model m) {
		m.addAttribute("msg", msg);
		return "common/success";
	}
	
	@RequestMapping("/my")
	public String myPage( Model m) {
		String userId = "c58eac281b6345038f623107dc69f683";
		BasicInfo basicInfo = basicInfoService.getBasicInfoById(userId);
		if(null!=basicInfo) {
			PicInfo pic =picInfoService.getPicInfoById(basicInfo.getPicId());
			if(null!=pic) {
				m.addAttribute("picUrl", pic.getUrl());
			}
		}
		m.addAttribute("info", basicInfo);
		return "personal/personal-my";
	}
	
	@RequestMapping("/file")
	public String file( ) {
		return "file-test";
	}
	
	@RequestMapping("/file/submit")
	@ResponseBody
	public String submit(HttpServletRequest req) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
		Iterator<String> fileNames = multipartRequest.getFileNames();
		MultipartFile file = multipartRequest.getFile(fileNames.next());
		String originalName = file.getOriginalFilename();// 源文件名称
		System.out.println(originalName);
		return originalName;
	}
}
